package com.example.javabasic.thread.classtwo.threadsynclist;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

/**
 * @description 线程阻塞列表
 * @Author Lin FuYuan
 * @date 2021/7/12
 */
public class ThreadSyncList {

    /**
     * 默认大小
     */
    private static final Integer DEFAULT_SIZE = 5;
    /**最大长度*/
    private static final Integer MAX_SIZE = 20;
    /**存储空间*/
    private LinkedList<Integer> LIST = new LinkedList<>();
    /**已使用*/
    private static AtomicInteger USED = new AtomicInteger(0);

    private static  AtomicInteger number = new AtomicInteger(0);

    public ThreadSyncList(){
        for (Integer i = 0; i < DEFAULT_SIZE; i++) {
            LIST.addLast(number.addAndGet(1));
        }
    }



    public Integer get(){
        synchronized (LIST){
            while(LIST.size()<=0){
                //当使用数量小于最大长度, 则添加新的内容
                if(USED.get()<MAX_SIZE){
                    LIST.addLast(number.addAndGet(1));
                }else{
                    try {
                        LIST.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            USED.addAndGet(1);
            Integer first = LIST.removeFirst();
            return first;
        }
    }
    /**已使用*/
    public Integer used(){
        return USED.get();
    }
    /**list长度*/
    public Integer size(){
        return LIST.size();
    }
    /**插入新数据,如果超出长度 跳过*/
    public Integer set(){
        synchronized (LIST){
            if(LIST.size()<=MAX_SIZE){
                if(LIST.size()<DEFAULT_SIZE){
                    //如果LIST长度小于默认长度则直接添加到LIST 中,
                    int i = number.addAndGet(1);
                    LIST.addLast(i);
                    //使用数量-1
                    USED.addAndGet(-1);
                    LIST.notifyAll();
                    return i;
                }else{
                    //不做任何处理  模拟丢弃
                    LIST.notifyAll();
                    return -1;
                }

            }
            return -2;
        }
    }

    public static void main(String[] args) {
        ThreadSyncList threadSyncList = new ThreadSyncList();
        //模拟获取链接
        new Thread(()->{
            while(true){
                Integer integer = threadSyncList.get();
                System.out.println("获取>>>>>>>  "+Thread.currentThread().getName()+"-> "+integer +" 已使用数量:"+threadSyncList.used()+" list长度:"+threadSyncList.size());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //阻塞线程
//                LockSupport.park();
            }
        },"获取线程").start();



        //模拟还回链接
        new Thread(()->{
            while(true){
                Integer set = threadSyncList.set();
                System.out.println("返还>>>>>>>  "+Thread.currentThread().getName()+"-> "+set +" 已使用数量:"+threadSyncList.used()+" list长度:"+threadSyncList.size());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"添加线程").start();
//    唤醒指定线程
        LockSupport.unpark(new Thread());
    }

}
